﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YJYpublicClass.DataStruct
{
    public class ResultFunc
    {
        public Funstate State { get; set; } = 0;
        public string mes { get; set; } = "";
        public ResultFunc() { }
        public ResultFunc(Funstate state, string Mes)
        {
            State = state;
            mes = Mes;

        }
    }

    public class ErrReigionRs
    {
        public int r1 { get; set; } = 0;
        public int r2 { get; set; } = 0;
    }

    public enum Funstate { 异常 = -1, OK = 0 };

    //方便频繁多次调用的参数，有些是固定参数，有些是频繁修改读写的参数。
    //固定调试次数少的参数，可以用xml，频繁调用的参数，宜用ini，xml参数文件经常出现损坏的情况。




    public class IniString_C
    {

        public string Value { get; set; } = "";
        public string _分段 { get; set; } = "系统";
        public string _变量名 { get; set; } = "默认";
        public string _默认值 { get; set; } = "";
        public string _保存文件 { get; set; } = "初始化.ini";
        public IniString_C() { }
        public IniString_C(string _分段t, string _变量名t, string _默认值t)
        {
            _分段 = _分段t;
            _变量名 = _变量名t;
            _默认值 = _默认值t;
        }
        public IniString_C(string _分段t, string _变量名t, string _默认值t, string _文件名)
        {
            _分段 = _分段t;
            _变量名 = _变量名t;
            _默认值 = _默认值t;
            _保存文件=_文件名;
        }

        public void save(string filepath)
        {
            PublicFuntions.WriteINI(_分段, _变量名, Value, filepath);
        }
        public void save()
        {
            string filepath = Environment.CurrentDirectory + "//" + _保存文件;
            PublicFuntions.WriteINI(_分段, _变量名, Value, filepath);
        }
        public void load(string filepath)
        {
            Value= PublicFuntions.GetINI(_分段, _变量名, _默认值, filepath);
        }
        public void load()
        {
            string filepath = Environment.CurrentDirectory + "//" + _保存文件;
            Value = PublicFuntions.GetINI(_分段, _变量名, _默认值, filepath);
        }
        public Boolean toBoolen()
        {
            try
            {
                return Convert.ToBoolean(Value);
            }
            catch (Exception)
            {
                return false;
            }
        }
        public int toInt()
        {
            try
            {
                return Convert.ToInt32(Value);
            }
            catch (Exception)
            {
                return 0;
            }
        }
        public double toDouble()
        {
            try
            {
                return Convert.ToDouble(Value);
            }
            catch (Exception)
            {
                return 0.0;
            }
        }
        public DateTime toDateTime()
        {
            try
            {
                return Convert.ToDateTime(Value);
            }
            catch (Exception)
            {
                return DateTime.Now;
            }
        }
        // 日期转换异常返回自定义日期时间
        public DateTime toTryDateTime(string defaultDate)
        {
            try
            {
                return Convert.ToDateTime(Value);
            }
            catch
            {
                return Convert.ToDateTime(defaultDate);
            }
        }

        public IniString_C setValue(object value)
        {
            Value = value.ToString();
            if (value is int)
            {
                Value = value.ToString();
            }
            return this;
        }
    }

    public class _款式索引
    {
        public string _款式名 { get; set; } = "新款式";
        public int _款式序号 { get; set; } = 1;
        public _款式索引()
        {

        }
        public _款式索引(string name, int Index)
        {
            _款式名 = name;
            _款式序号 = Index;
        }
    }

    //状态型变量，自动记录上升沿，下降沿，前次状态
    //public class _BOOL状态记录
    //{
    //    Boolean _value;
    //    public Boolean _上升沿 = false;
    //    public Boolean _下降沿 = false;
    //    Boolean _lastvalue;
    //    public Boolean _当前值
    //    {
    //        get
    //        { return _value; }
    //        set
    //        {
    //            _value=value;
    //            if (_lastvalue!=value)
    //            {
    //                if (value)
    //                {
    //                    _上升沿 =true;
    //                }
    //                else
    //                {
    //                    _下降沿 =false;
    //                }
    //                _lastvalue= value;
    //            }
    //        }

    //    }


    //}

    public class _obj状态记录
    {
        object _value;
        public Boolean _上升沿 = false;
        public Boolean _下降沿 = false;
        protected object _lastvalue;
        public object _当前值
        {
            get
            { return _value; }
            set
            {
                _value=value;

                if (_lastvalue!=value && _lastvalue !=null)
                {
                    if (value is bool)
                    {
                        _变化判断B(value);
                    }
                    if (value is int)
                    {
                        _变化判断I(value);
                    }

                }
                _lastvalue= value;
            }

        }
        public void _变化判断B(object value)
        {
            Boolean _值 = (Boolean)value;
            if (_值)
            {
                _上升沿 =true;
            }
            else
            {
                _下降沿 =true;
            }
        }
        public void _变化判断I(object value)
        {
            int _值 = (int)value;
            int _lastzhi = (int)_lastvalue;
            if (_值>_lastzhi)
            {
                _上升沿 =true;
            }
            else
            {
                _下降沿 =true;
            }
        }

        public bool tobool()
        {
            return (Boolean)_value;
        }
        public int toINT()
        {
            return (int)_value;
        }
        public void setvalue(object value)
        {
            _当前值 = value;
        }

    }

    //public class _BOOL状态记录2: _obj状态记录
    //{
    //    public override void _变化判断(object value)
    //    {
    //        Boolean _值 = (Boolean)value;
    //       if(_值)
    //        {
    //            _上升沿 =true;
    //        }else
    //        {
    //            _下降沿 =true;
    //        }
    //    }
    //}
    //public class _Int状态记录2 : _obj状态记录
    //{
    //    public override void _变化判断(object value)
    //    {
    //        int _值 = (int)value;
    //        int _lastzhi = (int)_lastvalue;
    //        if (_值>_lastzhi)
    //        {
    //            _上升沿 =true;
    //        }
    //        else
    //        {
    //            _下降沿 =true;
    //        }
    //    }
    //}
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [EditorBrowsable(EditorBrowsableState.Always)]
    public class DiZhi_C
    {

        public string Type { get; set; } = "M";
        public int dizhi { get; set; } = 0;
        public int pianyi { get; set; } = 1;
        public string _注释 { get; set; } = "";
        public _obj状态记录 _测量值 { get; set; } = new _obj状态记录();
        public DiZhi_C() { }
        public DiZhi_C(string type, int dizhi, int pianyi, string 注释)
        {
            Type = type;
            this.dizhi = dizhi;
            this.pianyi = pianyi;
            _注释 = 注释;
        }
        public string getAddressOffset(int index)
        {
            return (dizhi + pianyi * index).ToString();
        }
        public DiZhi_C(string type, int dizhi)
        {
            Type = type;
            this.dizhi = dizhi;
           
        }
    }



}
